package com.probiz.estoresa.system.web.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.validation.BindException;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.servlet.ModelAndView;

import com.probiz.estore.aop.VerFuncInterceptor;
import com.probiz.estore.common.model.system.VerFunc;
import com.probiz.estore.core.controller.GenericStoreAdminController;
import com.probiz.estore.core.exception.ApplicationException;
import com.probiz.estore.core.model.BaseObject;
import com.probiz.estore.core.search.SearchCriteria;
import com.probiz.estore.system.service.VerFuncManager;

public class VerFuncController extends GenericStoreAdminController<VerFunc> {
    private VerFuncManager verFuncManager = null;
    private String	onlyView = "/system/verFuncView";
    public void setVerFuncManager(VerFuncManager inMgr) {
        this.verFuncManager = inMgr;
    }
    
    public ModelAndView search(HttpServletRequest request,
			HttpServletResponse response) {
		SearchCriteria sc = createSearchCriteria(request);
		List results = searchByCriteria(sc);
		Object obj = request.getSession().getAttribute("VERFUNC_ADMIN");
		String admin = obj==null?"0":obj.toString();
		if(obj==null&&StringUtils.isNotBlank(request.getParameter("admin"))){
			admin="1";
			request.getSession().setAttribute("VERFUNC_ADMIN", admin);
		}
		if(admin.equalsIgnoreCase("0")){
			return getModelAndView(onlyView, listModelName, results);
		}else{
			return getModelAndView(listView, listModelName, results);
		}
	}

    //禁用
    public ModelAndView offFunc(HttpServletRequest request, HttpServletResponse response) {
		VerFunc verFunc = formBackingObject(request);
    	verFunc.setFuncValue(VerFuncInterceptor.encodeVerFuncValue(verFunc.getFuncKey(), "off"));
    	mgr.update(verFunc);
		return defaultAction(request, response);
	}
    
    //开放
    public ModelAndView onFunc(HttpServletRequest request, HttpServletResponse response) {
    	VerFunc verFunc = formBackingObject(request);
    	verFunc.setFuncValue(VerFuncInterceptor.encodeVerFuncValue(verFunc.getFuncKey(), "on"));
    	mgr.update(verFunc);
		return defaultAction(request, response);
	}
    
    public ModelAndView edit(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		// 取得Form对应的Model
    	VerFunc entity = formBackingObject(request);
    	//进行必要的解码以便页面显示
    	entity.setFuncTitle(VerFuncInterceptor.getVerFuncTitle(entity.getFuncKey()));
    	entity.setFuncKey(VerFuncInterceptor.getVerFuncKey(entity.getFuncKey()));
    	entity.setFuncValue(VerFuncInterceptor.getVerFuncValue(entity.getFuncValue()));
    	
		ServletRequestDataBinder binder = this.getDefaultBinder(request,entity);
		BindException errors = new BindException(binder.getBindingResult());
		return showForm(request, errors);
	}
    
    public ModelAndView save(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		// 取得Form对应的Model
    	VerFunc entity = formBackingObject(request);
		BindException errors = null;
		try {
			ServletRequestDataBinder binder = bindAndValidate(request, entity);
			errors = new BindException(binder.getBindingResult());
			// 传给子类的实现，后者可以继续验证和加入错误到errors
			preSave(request, entity, errors);
			if (!errors.hasErrors()) {// 里面如果出错应该抛出异常
				//进行必要的编码再存储
				entity.setFuncKey(VerFuncInterceptor.encodeVerFuncKey(entity.getFuncKey()));
		    	entity.setFuncValue(VerFuncInterceptor.encodeVerFuncValue(entity.getFuncKey(), entity.getFuncValue()));
				mgr.save(entity);
				postSave(request, entity);
			}
		} catch (ApplicationException e) {
			handleApplicationException(errors, e);
		}

		ModelAndView mav;
		if (errors.hasErrors()) {
			mav = showForm(request, errors);
		} else if (successView != null) {
			mav = getModelAndView(successView, errors.getModel());
		} else {
			mav = getRedirectToActionView("edit", ((BaseObject) entity).getId()
					.toString());
		}
		return mav;
	}
    
    protected void postSave (HttpServletRequest request, VerFunc entity){
		//保存操作成功的提示信息
		String msgKey = (isEntityNew(request)) ? "verFunc.add.message"	: "verFunc.save.message";
		String message = getMessage(msgKey);
		saveMessage(request, message);
	}
    
    protected void postDelete (HttpServletRequest request, String entityName){
		String message = getMessage("verFunc.delete.message");
		saveMessage(request, message);
	}
    
	/*
	 * (non-Javadoc)
	 * 
	 * @see com.probiz.estore.core.controller.GenericController#getEntityName(java.lang.Object)
	 */
	@Override
	protected String getEntityName(VerFunc entity) {
		return entity.getVerFuncName();
	}


	/*
	 * (non-Javadoc)
	 * 
	 * @see com.probiz.estore.core.controller.BaseController#initController()
	 */
	@Override
	protected void initController() throws Exception {
		mgr = verFuncManager;
		defaultPageSize = 1000;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.probiz.estore.core.controller.GenericController#preSave(javax.servlet.http.HttpServletRequest,
	 *      java.lang.Object, org.springframework.validation.BindException)
	 */
	@Override
	protected void preSave(HttpServletRequest request, VerFunc entity, BindException errors) {
	}

}
